<template>
	<view>
		<image class="login_bg" src="/static/a.jpg" mode="aspectFill"></image>
		<view class="logo-wrap">
			<image src="/static/logo.png" style="width: 130rpx;height:130rpx" mode="aspectFill" class="logo"></image>
			<view class="name">蚂蚁玩家聚集地</view>
		</view>
		
		
		<view class="cell">
			<view class="label" style="z-index: 777;">手机号</view>
			<input type="number" maxlength="11" v-model="form.mobile" class="input" placeholder="请输入手机号" />
			
			<button @click="getCode" :disabled="codeDisabled" :style="codeDisabled?'':'background: #333;color: #fff;'"  class="btn-code">{{ codeText }}</button>
		</view>

		<view class="cell">
			<view class="label"  style="z-index: 777;">验证码</view>
			<input v-model="form.code" maxlength="6" class="input" type="text" placeholder="请输入验证码" />
		</view>

		<view class="tips">需要先使用微信登录，在【我的】-【设置】中绑定手机号后，才能使用手机号登录</view>

		<!-- <view class="fixed-wrap">
			<button @click="login()" :disabled="loginDisabled"  :style="codeDisabled?'':'background: #333;color: #fff;'"  class="btn-login">登录</button>
		</view> -->
		
		<view class="btns">
			<button @click="loginHandle" class="btn" type="default" >
				<!-- <image src="/static/wx_login.png" class="wx_icon"></image> -->
				<text>确认登录</text>
			</button>
		</view>
		
		<view class="pact-wrap">
			<fui-label class="pact-checkbox">
				<fui-checkbox
					@change="checkboxChange"
					scaleRatio="0.9"
					value="1"
					:checked="checked"
					color="#e6d9b6"
					checkMarkColor="#000"
					borderRadius="8rpx"></fui-checkbox>
				<view class="pact-label">
					<text class="pact-text">同意蚂蚁游</text>
					<text class="pact-em" @click="tz('/pages/index/xieyi1?id=2')">《用户协议》</text>
					<text class="pact-em" @click="tz('/pages/index/xieyi1?id=1')">《隐私政策》</text>
				</view>
			</fui-label>
		</view>
		
		<slideCode v-if="slideCode[0].show" :session_id="session_id[0]" :abcde='1' :phone="phone" @close="slideCode[0].show = false"
			@success="slideCode_success(0, $event)" ref="abcd"></slideCode>
	</view>
</template>

<script>
	import slideCode from '@/components/slideCode1.vue'
	import { isMobile } from '@/utils/validator.js';

	const SECONDS = 60;

	export default {
		components: {slideCode},
		data() {
			return {
				form: {
					mobile: '',
					code: '',
					invite_code:""
				},
				checked:false,
				seconds: SECONDS,
				codeText: '获取验证码',
				counting: false,
				timer: null,
			
				codeStatus: true, //禁止重复点击
				invite_code: '', //邀请码
				
				seconds: 60,
				startText: '发送验证码',
				session_id: [], //会自动生成的
				slideCode: [{
					show: false
				}],
				phone:"",
			};
		},
		onLoad() {
			if(uni.getStorageSync('inviteCode')) {
				this.invite_code = uni.getStorageSync('inviteCode');
			}
		},
		computed: {
			loginDisabled() {
				return !(this.form.mobile && this.form.code);
			},
			codeDisabled() {
				return !(this.form.mobile);
			}
		},
		methods: {
			jumpLoginMobile() {
				if (!this.checked) {
					return uni.showToast({
						title: '请仔细阅读并同意潮玩宇宙相关协议',
						icon: 'none'
					});
				}
				uni.navigateTo({
					url: '/pages/login-mobile/login-mobile'
				});
			},
			
			// 协议checkbox
			checkboxChange(e) {
				this.checked = e.checked;
			},
			tz(url){
				uni.navigateTo({ url: url});
			},
			// 登录
			loginHandle() {
				if (!this.checked) {
					return uni.showToast({
						title: '请仔细阅读并同意潮玩宇宙相关协议',
						icon: 'none'
					});
				}
				this.login()
			},

			// 获取验证码
			// getCode() {
				
			// },

			// 倒计时
			countdown() {
				this.counting = true;
				this.codeText = this.seconds + ' s';
				this.timer = setInterval(() => {
					if (this.seconds > 0) {
						this.seconds--;
						this.codeText = this.seconds + ' s';
					} else {
						this.counting = false;
						this.codeText = '再次获取';
						this.seconds = SECONDS;
						clearInterval(this.timer);
						this.timer = null;
					}
				}, 1000);
			},
			login() {
				if (!isMobile(this.form.mobile)) {
					return uni.showToast({
						title: '手机号码有误',
						icon: 'none'
					});
				}
				
				console.log(777777777)
				
				var _this = this;
				 if (!this.form.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
				}else {
					this.$api.request('/api/user/mobilelogin1', this.form, '', function(res){
						console.log(res);
						if(res.code == 1) {
							console.log( res.data.userinfo,6666666)
							 uni.setStorageSync('userinfo', res.data.userinfo);
							 uni.setStorageSync('token',  res.data.userinfo.token);
							
					
								console.log(111111)
								// uni.switchTab({
								// 	url: '/pages/index/index'
								// });
								
								uni.navigateTo({ url: "/pages/index/shop"});
						
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					});
				}
			},
			getCode() {
				if (!isMobile(this.form.mobile)) {
					return uni.showToast({
						title: '手机号码有误',
						icon: 'none'
					});
				}
				
				// if (!isMobile(this.form.mobile)) {
				// 	return uni.showToast({
				// 		title: '手机号码有误',
				// 		icon: 'none'
				// 	});
				// }
				// this.countdown();
					
				this.phone = this.form.mobile
				this.session_id[0] = this.getRandomNunber(0,999999999);//唯一id
				this.slideCode[0].yanzheng = false;//图形验证码是否验证通过
				this.slideCode[0].x	= 0//图形验证码的水平偏移值
				// this.$slideCode_init(0);//初始化图形滑块
				this.slideCode[0].show = true
				this.slideCode[0].abcde = 1;
				this.slideCode[0].phone = this.form.mobile
				
				// if (this.$refs.uCode.canGetCode) {
				// 	// 模拟向后端请求验证码
				// 	uni.showLoading({
				// 		title: '正在获取验证码'
				// 	});
				// 	setTimeout(() => {
				// 		uni.hideLoading();
				// 		// 这里此提示会被this.start()方法中的提示覆盖
				// 		uni.$u.toast('验证码已发送');
				// 		// 通知验证码组件内部开始倒计时
				// 		this.$refs.uCode.start();
				// 	}, 2000);
				// } else {
				// 	uni.$u.toast('倒计时结束后再发送');
				// }
			},
			codeChange(text) {
				this.tips = text;
			},
			getRandomNunber (start,end) {
				return Math.floor(Math.random() * (end - start) + start)
			},
			slideCode_init (index) {
				this.session_id[index] = this.getRandomNunber(0,999999999);//唯一id
				this.slideCode[index].yanzheng = false;//图形验证码是否验证通过
				this.slideCode[index].x	= 0//图形验证码的水平偏移值
				this.slideCode[index].abcde = 1;
			},
			slideCode_success (index, data) {
				this.slideCode[index].yanzheng = true//验证通过
				this.slideCode[index].x	= data.x//偏移值
				this.slideCode[index].abcde = 1;
				this.yzm(index)//获取验证码
			},
			yzm(index) {
				
				let that = this;
				
				if (this.slideCode[index].yanzheng === true) { //图形滑块是否验证通过
		
				
						
						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						});
						
						setTimeout(() => {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							that.countdown();
						}, 1000);
					
					
				} else {
					this.slideCode[index].show = true
				}
			}
		}
	};
</script>

<style lang="less">
	.logo-wrap {
		padding-top: 200rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;
	
		.logo {
			width: 130rpx;
			height: 130rpx;
			margin-bottom: 48rpx;
			border-radius: 0.5em;
			image{
				width: 130rpx;
				height: 130rpx;
			}
		}
	
		.name {
			font-size: 30rpx;
			color: #fff;
			letter-spacing: 8rpx;
		}
	}
	.login_bg {
		width: 100%;
		height: 100%;
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
	}
	.cell {
		display: flex;
		align-items: center;
		padding: 16rpx 32rpx;
		border-bottom: 1px solid #f3f3f3;

		.label {
			font-size: 32rpx;
			font-weight: 600;
			width: 100rpx;
			color: #fff;
		}

		.input {
			flex: 1;
			height: 100rpx;
			color:#fff ;
			padding: 0 32rpx;
		}

		.btn-code {
			font-size: 24rpx;
			background: #333;
			color: #fff;
			width: 200rpx;
		}

		.input-placeholder {
			color: #ccc;
		}
	}

	.tips {
		font-size: 26rpx;
		color: #999;
		padding: 64rpx 32rpx;
	}

	.fixed-wrap {
		position: fixed;
		left: 48rpx;
		right: 48rpx;
		bottom: 100rpx;
		.btn-login {
			background: #333;
			color: #fff;
			font-weight: bold;
			height: 100rpx;
			line-height: 100rpx;
		}
	}
	button .btn-login{
		background: red;
	}
	button[disabled].btn-login {
		background: #e6e6e6;
	}

	button[disabled].btn-code {
		background: #e6e6e6;
	}


		
	button::after {
		border: none;
	}
	.pact-wrap {
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 120rpx;
	
		.pact-checkbox {
			display: flex;
			align-items: center;
		}
	
		.pact-label {
			color: #fff;
			font-size: 26rpx;
			margin-left: 12rpx;
		}
	
		.pact-text {
			color: rgba(255, 255, 255, 0.7);
		}
	
		.checkbox {
			transform: scale(0.7);
		}
	}
	
	.btns {
		position: relative;
		width: 90%;
		margin-left: 5%;
		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32rpx;
			font-weight: bold;
			width: 100%;
			padding: 16rpx 0;
			box-sizing: border-box;
		}
	
		.wx_icon {
			width: 48rpx;
			height: 48rpx;
			margin-right: 16rpx;
		}
	
		.btn-mobile {
			display: flex;
			justify-content: center;
			color: rgba(255, 255, 255, 0.7);
			font-size: 30rpx;
			margin: 32rpx;
		}
	}
</style>
